<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height="100%"固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<!-- ref动态生成: 字节跳动小程序编辑器不支持一个页面存在相同的ref (如不考虑字节跳动小程序可固定值为 ref="mescrollRef") -->
	<mescroll-uni ref="mescrollRef" height="100%" @init="mescrollInit" bottom="60" :down="downOption"
		@down="downCallback" :up="upOption" @up="upCallback">
		<!-- 数据列表 -->
		<view class="item-wrapper" v-for="(item,indexs) in list" :key="indexs">
			<view class="date-wrapper-box" :class="{'first':indexs === 0}">
				<view class="date-info">
					<view class="date" @click="openDate(item.date)">
						<text>{{ item.date }}</text>
						<uni-icons type="bottom" size="16" color="#888888"></uni-icons>
					</view>
					<!-- <view class="misc">
						{{ miscStr(item.date) }}
					</view> -->
				</view>
			</view>
			<view class="item" v-for="(items,indexss) in item.children" :key="indexss">
				<productItem :index="indexss" :source="items"></productItem>
			</view>
		</view>
		<!-- <uni-load-more v-if="list.length>1" :status="status" color="#1EAF22" :icon-size="16" style="margin-bottom: 20upx;" :content-text="contentText" /> -->
		<!-- <view class="nodata-wrapper" v-if="!list.length">
			<view class="loading-wrapper loading-image" style="position: relative;display: inline-block;" v-if="isLoading">
				<zero-loading  type="love"  position="absolute"></zero-loading>
			</view>
			<image v-else src="../../../../static/image/noorder.png" class="nodata-image" mode="widthFix"></image>
			<view class="text">
				 {{isLoading?'数据加载中...':'暂无订单'}}
			</view>
		</view> -->
		<mx-date-picker :show="showPicker" type="date" format="yyyy-mm-dd" :end="today" :value="date" :show-tips="false"
			@confirm="confirmDate" @cancel="showPicker=false" />
	</mescroll-uni>
</template>

<script>
	import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js";
	import productItem from '@/subPage_distribution/productItem.vue'

	export default {
		mixins: [MescrollMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		data() {
			return {
				downOption: {
					auto: true // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					textNoMore: '没有更多啦~',
					noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						fixed: true,
						top: '200upx',
						tip: '~ 空空如也 ~', // 提示
						icon: 'https://sm.hobon.cc/static/hobon_noorder.png'
					}
				},
				isLoading: true,
				//mescroll:'',
				list: [],
				pageNum: 1,
				pageSize: 10,
				date: '',
				showPicker: false,
				today: this.$helper.getDay(3, '-')
				//org: this.$store.state.userInfo.orgRelation.org,
			}
		},
		props: {
			//i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			// date: {
			// 	type: String,
			// 	default () {
			// 		return ''
			// 	}
			// },
			keywords: {
				type: String,
				default () {
					return ''
				}
			}
		},
		methods: {
			openDate(date) {
				console.log("data", date)
				this.date = date;
				this.showPicker = true;
			},
			confirmDate(e) {
				this.date = e.value;
				this.showPicker = false;
				this.refresh()
			},
			dateStr(date) {
				let str = '';
				if (date === this.$helper.getDay(1, '-')) {
					str = '明天 ' + date
				} else if (date === this.$helper.getDay(0, '-')) {
					str = '今天 ' + date
				} else if (date === this.$helper.getDay(-1, '-')) {
					str = '昨天 ' + date
				} else {
					str = date
				}
				return str;
			},
			refresh() {
				this.mescroll.scrollTo(0, 0)
				this.$nextTick(() => {
					this.mescroll.resetUpScroll()
				})
			},
			refreshData(data) { //局部刷新数据

			},
			// mescrollInit(mescroll) {
			// 	this.mescroll = mescroll;
			// },
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.date = '';
				this.mescroll.resetUpScroll()
			},
			getData() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			async upCallback(page) {
				this.pageNum = page.num;
				const params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					pickupPointId: this.$store.state.userInfo.pickupPointId,
					mayDeliverDate: this.date,
					productName: this.keywords,
					orders: [{
						asc: false,
						column: 'id'
					}],
				};
				const data = await this.$api.post('/app/supply/record/sortProductPage', params, false)
				if (data.code === 200) {
					this.isLoading = false;
					if (this.pageNum === 1) {
						this.list = [];
					}
					data.data.records.forEach(item => {
						item.productImage =
							"https://cdpresources.hobon.cc/supplier-mini-program/static/noProduct.png";
						if (item.mainImageVideo) {
							const mainImageVideo = JSON.parse(item.mainImageVideo)[0]
							if (mainImageVideo.type === 0) {
								item.productImage = mainImageVideo.url + '?imageView2/3/w/300/q/100'
							} else {
								item.productImage = mainImageVideo.url + '?vframe/jpg/offset/1'
							}
						}

						const mayDeliverDate = item.mayDeliverDate
						const objIndex = this.list.findIndex(row => row.date === mayDeliverDate)
						if (objIndex > -1) {
							this.list[objIndex].children.push(item)
						} else {
							this.list.push({
								date: mayDeliverDate,
								// orderAmount: item.orderAmount,
								// orderNum: item.orderNum,
								// orderQuantity: item.orderQuantity,
								children: [{
									...item
								}]
							})
						}
					})
					//this.list = this.list.concat(data.data.records);
					this.mescroll.endByPage(data.data.records.length, data.data.pages);
				} else {
					this.mescroll.endErr();
				}
			},
			//点击空布局按钮的回调
			emptyClick() {
				uni.showToast({
					title: '点击了按钮,具体逻辑自行实现'
				})
			}
		},
		components: {
			productItem,
			MxDatePicker
		}
	}
</script>
<style lang="scss" scope>
	.item-wrapper {
		.date-wrapper-box {
			background-color: #ffffff;
			height: 80upx;
			padding: 0 30upx;
			margin-top: 20rpx;
			position: sticky;
			top: 0upx;
			border-top: 1upx solid #E6EDF2;
			border-bottom: 1upx solid #E6EDF2;
			display: flex;
			justify-content: center;
			flex-direction: column;

			&.first {
				margin-top: 0upx;
			}

			.date-info {
				display: flex;
				align-items: center;

				.date {
					font-size: 32upx;
					font-weight: bold;
					color: #313131;
				}

				.misc {
					font-size: 28rpx;
					color: #666666;
					margin-left: 30rpx;
				}
			}

			.total {
				font-size: 26upx;
				color: #666666;
				margin-top: 10rpx;
			}
		}
	}

	.item {
		margin: 0 20upx;
		border-radius: 20upx;
		background-color: #ffffff;
		margin-top: 20upx;
	}

	.nodata-wrapper {
		text-align: center;
		font-size: 32upx;
		color: #666666;

		.nodata-image {
			margin-top: 300upx;
			margin-bottom: 10upx;
			width: 246upx;
		}

		.loading-image {
			margin-top: 300upx;
			margin-bottom: 10upx;
			width: 296upx;
			height: 205upx;
		}
	}
</style>